<?php 
session_start();
include "dbconnect.php";
if(!isset($_SESSION['Username']))
{
  header("location:index.php");

}
else 
{
	if($_SESSION['Type']!=2)
  header("location:unauthorized.php");
	//echo isset($_SESSION['Username']);
	
}
$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
if(!$con)
{
	die("Error: ".mysql_error());
}
mysql_select_db($_DB_NAME,$con);
$post=0;
$yearerr="";
$year="";
//validation needs to be handled
if ($_SERVER["REQUEST_METHOD"] == "POST")
	{
	$semType=$_POST['semesterType'];
		$openingYear=$_POST['semYear'];
	
	if (empty($openingYear))
    	{
			$yearerr = "Year is required";
		}
  		else
    	{   // $sem = clean_input($semType);
    		$year = clean_input($openingYear);
				if (!preg_match("/^[0-9]{4}$/",$year))
			   {
					  $yearerr = "Invalid Year format";
			   }
				else
			   {
					$post=1;
					mysql_query("set @semtypte='".$semType."'");
					mysql_query("set @openyear=".$openingYear);
					$semidrs = mysql_query("select semid from semester where term=@semtypte and year=@openyear");
					if($semidrs == false )
					{
						die(mysql_error() );}
					$semid = mysql_fetch_assoc($semidrs);
					
					mysql_query("set @semid=".$semid["semid"]);
					mysql_query("set @username='".$_SESSION['Username']."'");
					$result = mysql_query("select * from openings o,courses c where semid=@semid and instructor=@username and o.courseid=c.courseid and isdeleted=0");	 
					if($result == false)
						{
							die(mysql_error());
						}
				}
		}

	}
function clean_input($data)
	{
	  $data = trim($data);
	  $data = stripslashes($data);
	  $data = htmlspecialchars($data);
	  return $data;
	}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comment</title>
<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />

<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>

<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/jquery-ui-1.10.3/jquery-ui-1.10.3/ui/jquery-ui.js"></script>
  
	 <script type="text/javascript">
		function populateYear()
		{
			$.ajax({
						type: "POST",
                		url: "PopulateYear.php",
                		data: {term :document.getElementById("term").value,year: document.getElementById("year").value},
					   success: function(courses){
									$("#year").autocomplete({
        								source: courses.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");
							   }
					});
		}
		</script>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
    $('#datatables').dataTable(
	{	"bJQueryUI":true
		});
});
</script>

<script>
function set(openingid)
{	

	 //var hashid=CryptoJS.SHA256(openingid);
	  window.location.href = 'comment.php?openingid='+openingid 

 	}
</script>	
<!--<script>
$('#semYear').change()(function(){
	$.ajax({
						type: "POST",
                		url: "yearSuggest.php",
                		data: {Semester: 'SPRING',Year:20},
					    success: function(yearparam){
									$("#semYear").autocomplete({
        								source: yearparam.split(',')
    								});
								 },
						error: function(){
							   	alert("Error");	}
								});
});
</script> -->
<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>
<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />

</head>

<body style="min-width:1280px">

<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img alt="Indiana University" height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="grey" >
</div>
	<center>
			<div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->
              <ul>
                <li style="width:200px;"><a href="professor.php">Comment</a></li>
                <li style="width:200px;" ><a href="profhire.php">Hiring</a></li>
              </ul>
      	</div>
        </center>
        <span style="width:100px;margin-left:300px;">
                <button class="hyperbutton" onclick="location.href = 'logout.php'" >logout</button>
         </span>
        <div class="footbody">
       
        <form method="post" action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"]));?>" >
          <table class="SemesterTable">
          <thead>
          <tr>
          <td>
           <label class="Header1"> AI Openings</label>
          </td>
          </tr>
          </thead>
        <tbody>
        	<tr />
            <tr />
            <tr />
            <tr />
            <tr />
            <tr />
         	  <tr>
                  <td>
                  	<label style="color:#990000" >Select a semester </label>
                  </td>
                  <td>
                      <select name="semesterType" class="crimson-text" id="term" >
                     <option value="Fall"<?php
					  if (isset($semType) && $semType=="Fall") echo "selected='selected'";
					  else echo "";?>>Fall</option>
                   <option value="Spring"<?php
					  if (isset($semType) && $semType=="Spring") echo "selected='selected'";
					  else echo "";?>>Spring</option>

                      </select>
                  </td>
                  </tr>
                  <tr />
            <tr />
            <tr />
            <tr />
            <tr />
            <tr />
                  <tr>
                  <td>
                    <label class="crimson-text"> Year</label>
                  </td>
                  <td>
                    <input type="text"  name="semYear" class="crimson-border" value="<?php echo $year;?>" id="year" onkeydown="populateYear()"/>
                  </td>
                  <td class="error">
     <?php echo $yearerr;?>
        </td>
                  </tr>
                  <tr />
            <tr />
            <tr />
            <tr />
            <tr />
            <tr />
                  <tr>
                  <td>
                    <input type="submit" value="View" />
                  </td>
                  </tr>
                  </tbody>
                  </table>
                  </form>
        <div class="datatable-container">
    <table id="datatables" class="display">
          <thead>
          <tr>
          <th>Coureid</th>
          <th>Course</th>
          <th>Total</th>
          <th>Filled</th>
          <th>Waiting</th>
          <th>Comment</th>
          </tr>
          </thead>
          <tbody>
          <?php 
		  if($post==1)
		  {
		  while($row=mysql_fetch_array($result)){
			  ?>
              <tr>
              <td ><?=$row['courseid']?></td>
              <td><?=$row['description']?></td>
              <td><?=$row['Total']?></td>
              <td><?=$row['Filled']?></td>
              <td><?=$row['Waiting']?></td>
              <td>
                <a  id=viewOpening data-value=<?php echo $row['openingid']?> onclick="set($(this).data('value'))">Comment</a>
                </td>
              </tr>
              <?php } }?>
          </tbody>
		</table>
       </div>
       </div>
</body>
</html>